package com.xthh.sql;


import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


import com.xthh._enum.HH_EnumLayerInfoINI;
import com.xthh._enum.HH_EnumLayerType;
import com.xthh._enum.HH_EnumTextAlgin;
import com.xthh._function.HH_CharUtil;
import com.xthh._struct.HH_T_LayerInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * api查询方式
 */
public class HH_LayerInfoDao2 {

    private static final String TAG = "LayerInfoDao2";
    HH_XTSQLiteOpenHelper mOpenHelper;//数据库的帮助类

    public HH_LayerInfoDao2(Context context, Application application) {
        mOpenHelper = new HH_XTSQLiteOpenHelper(context, application);
    }


    /**
     * 添加到BindDevice表一条数据
     */
    public void insert(HH_T_LayerInfo tr) {
        List<HH_T_LayerInfo> trFind = queryItem(tr.getModelID());

        if (trFind != null){
            //delete(tr.getModelID());
        }

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();

            values.put(HH_EnumLayerInfoINI.modelID.toString(), tr.getModelID());
            values.put(HH_EnumLayerInfoINI.x.toString(), HH_CharUtil.Itoa(tr.getX()));
            values.put(HH_EnumLayerInfoINI.y.toString(), HH_CharUtil.Itoa(tr.getY()));
            values.put(HH_EnumLayerInfoINI.w.toString(), HH_CharUtil.Itoa(tr.getW()));
            values.put(HH_EnumLayerInfoINI.h.toString(), HH_CharUtil.Itoa(tr.getH()));
            values.put(HH_EnumLayerInfoINI.fontName.toString(), tr.getFontName());
            values.put(HH_EnumLayerInfoINI.fontSize.toString(), HH_CharUtil.Itoa(tr.getFontSize()));
            values.put(HH_EnumLayerInfoINI.font_r.toString(), HH_CharUtil.Itoa(tr.getFont_r()));
            values.put(HH_EnumLayerInfoINI.font_g.toString(), HH_CharUtil.Itoa(tr.getFont_g()));
            values.put(HH_EnumLayerInfoINI.font_b.toString(), HH_CharUtil.Itoa(tr.getFont_b()));
            values.put(HH_EnumLayerInfoINI.bkg_r.toString(),  HH_CharUtil.Itoa(tr.getBkg_r()));
            values.put(HH_EnumLayerInfoINI.bkg_g.toString(),  HH_CharUtil.Itoa(tr.getBkg_g()));
            values.put(HH_EnumLayerInfoINI.bkg_b.toString(),  HH_CharUtil.Itoa(tr.getBkg_b()));
            values.put(HH_EnumLayerInfoINI.layerName.toString(), tr.getLayerName());
            values.put(HH_EnumLayerInfoINI.isBold.toString(), HH_CharUtil.BToS(tr.isBold()));
            values.put(HH_EnumLayerInfoINI.textAlgin.toString(), HH_CharUtil.Itoa(tr.getTextAlgin().ordinal()));
            values.put(HH_EnumLayerInfoINI.text.toString(), tr.getText());
            values.put(HH_EnumLayerInfoINI.layerAttr.toString(), HH_CharUtil.Itoa(tr.getLayerAttr().ordinal()));
            values.put(HH_EnumLayerInfoINI.picPath.toString(), tr.getPicPath());
            values.put(HH_EnumLayerInfoINI.picAttr.toString(), HH_CharUtil.BToS(tr.isPicAttr()));
            values.put(HH_EnumLayerInfoINI.picSrcWidth.toString(), HH_CharUtil.Itoa(tr.getPicSrcWidth()));
            values.put(HH_EnumLayerInfoINI.picSrcHeight.toString(), HH_CharUtil.Itoa(tr.getPicSrcHeight()));
            values.put(HH_EnumLayerInfoINI.rotate.toString(), HH_CharUtil.Itoa(tr.getRotate()));

            long id = db.insert(HH_SheetName.SQL_SHEET_NAME_LAYER_INFO, null, values);

            db.close(); //数据库关闭
        }

    }

    public void deleteAll() {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();//获得可写入数据库对象
        if (db.isOpen()) {
            db.execSQL("delete from " + HH_SheetName.SQL_SHEET_NAME_LAYER_INFO);
            db.close();
        }
    }

    public void delete(String modelID) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();//获得可写入数据库对象
        if (db.isOpen()) {
            String whereClasuse = HH_EnumLayerInfoINI.modelID.toString() + " = ?";
            String[] whereArgs = {modelID};
            int count = db.delete(HH_SheetName.SQL_SHEET_NAME_LAYER_INFO, whereClasuse, whereArgs);

            Log.i(TAG, "删除了:" + count);
            db.close();
        }
    }

    /*
    public void update_name(String mac, String name) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        if (db.isOpen()) {
            ContentValues values = new ContentValues();
            values.put(HH_EnumLayerInfoINI.name.toString(), name);
            String whereClause = HH_EnumLayerInfoINI.macRes.toString() + " = ?";
            int count = db.update(HH_SheetName.SQL_SHEET_NAME_LAYER_INFO, values, whereClause, new String[]{mac});
            db.close();
        }
    }*/


    public List<HH_T_LayerInfo> queryAll() {
        List<HH_T_LayerInfo> personList = new ArrayList<HH_T_LayerInfo>();

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumLayerInfoINI.values().length];
            for (HH_EnumLayerInfoINI e : HH_EnumLayerInfoINI.values()) {
                columns[e.ordinal()] = e.name();
            }
            String selection = null; // 选择条件，给null查询所有
            String[] selectionArgs = null;//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(HH_SheetName.SQL_SHEET_NAME_LAYER_INFO, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.getCount() > 0) {

                while (cursor.moveToNext()) { //向下移动一位，直到最后一位,不可以往下移动了，停止
                    HH_T_LayerInfo tr = new HH_T_LayerInfo();

                    tr.setModelID(cursor.getString(HH_EnumLayerInfoINI.modelID.ordinal()));
                    tr.setX(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.x.ordinal())));
                    tr.setY(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.y.ordinal())));
                    tr.setW(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.w.ordinal())));
                    tr.setH(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.h.ordinal())));
                    tr.setFontName(cursor.getString(HH_EnumLayerInfoINI.fontName.ordinal()));
                    tr.setFontSize(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.fontSize.ordinal())));
                    tr.setFont_r(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.font_r.ordinal())));
                    tr.setFont_g(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.font_g.ordinal())));
                    tr.setFont_b(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.font_b.ordinal())));
                    tr.setBkg_r(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.bkg_r.ordinal())));
                    tr.setBkg_g(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.bkg_g.ordinal())));
                    tr.setBkg_b(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.bkg_b.ordinal())));
                    tr.setLayerName(cursor.getString(HH_EnumLayerInfoINI.layerName.ordinal()));
                    tr.setPicPath(cursor.getString(HH_EnumLayerInfoINI.picPath.ordinal()));
                    tr.setBold(HH_CharUtil.SToB(cursor.getString(HH_EnumLayerInfoINI.isBold.ordinal())));
                    tr.setText(cursor.getString(HH_EnumLayerInfoINI.text.ordinal()));
                    tr.setPicSrcWidth(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.picSrcWidth.ordinal())));
                    tr.setPicSrcHeight(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.picSrcHeight.ordinal())));
                    tr.setTextAlgin(HH_EnumTextAlgin.values()[HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.textAlgin.ordinal()))]);
                    tr.setLayerAttr(HH_EnumLayerType.values()[HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.layerAttr.ordinal()))]);
                    tr.setPicAttr(HH_CharUtil.SToB(cursor.getString(HH_EnumLayerInfoINI.picAttr.ordinal())));
                    tr.setRotate(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.rotate.ordinal())));
                    personList.add(tr);
                }
                db.close();
                return personList;
            }

            db.close();
        }

        return personList;
    }


    public List<HH_T_LayerInfo> queryItem(String modelID) {
        List<HH_T_LayerInfo> personList = new ArrayList<HH_T_LayerInfo>();
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumLayerInfoINI.values().length];
            for (HH_EnumLayerInfoINI e : HH_EnumLayerInfoINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String selection = HH_EnumLayerInfoINI.modelID.toString() + " = ?"; // 选择条件，给null查询所有
            String[] selectionArgs = {modelID};//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(HH_SheetName.SQL_SHEET_NAME_LAYER_INFO, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.getCount() > 0) {

                while (cursor.moveToNext()) { //向下移动一位，直到最后一位,不可以往下移动了，停止
                    HH_T_LayerInfo tr = new HH_T_LayerInfo();

                    tr.setModelID(cursor.getString(HH_EnumLayerInfoINI.modelID.ordinal()));
                    tr.setX(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.x.ordinal())));
                    tr.setY(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.y.ordinal())));
                    tr.setW(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.w.ordinal())));
                    tr.setH(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.h.ordinal())));
                    tr.setFontName(cursor.getString(HH_EnumLayerInfoINI.fontName.ordinal()));
                    tr.setFontSize(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.fontSize.ordinal())));
                    tr.setFont_r(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.font_r.ordinal())));
                    tr.setFont_g(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.font_g.ordinal())));
                    tr.setFont_b(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.font_b.ordinal())));
                    tr.setBkg_r(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.bkg_r.ordinal())));
                    tr.setBkg_g(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.bkg_g.ordinal())));
                    tr.setBkg_b(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.bkg_b.ordinal())));
                    tr.setLayerName(cursor.getString(HH_EnumLayerInfoINI.layerName.ordinal()));
                    tr.setPicPath(cursor.getString(HH_EnumLayerInfoINI.picPath.ordinal()));
                    tr.setBold(HH_CharUtil.SToB(cursor.getString(HH_EnumLayerInfoINI.isBold.ordinal())));
                    tr.setText(cursor.getString(HH_EnumLayerInfoINI.text.ordinal()));
                    tr.setPicSrcWidth(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.picSrcWidth.ordinal())));
                    tr.setPicSrcHeight(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.picSrcHeight.ordinal())));
                    tr.setTextAlgin(HH_EnumTextAlgin.values()[HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.textAlgin.ordinal()))]);
                    tr.setLayerAttr(HH_EnumLayerType.values()[HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.layerAttr.ordinal()))]);
                    tr.setPicAttr(HH_CharUtil.SToB(cursor.getString(HH_EnumLayerInfoINI.picAttr.ordinal())));
                    tr.setRotate(HH_CharUtil.Atoi(cursor.getString(HH_EnumLayerInfoINI.rotate.ordinal())));
                    personList.add(tr);
                }
                db.close();
                return personList;
            }

            db.close();
        }

        return null;
    }

}